﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
namespace EC_Socket
{
    class Client
    {
        protected const int SERVER_PORT = 5000;
        protected const int SIZE_OF_PACKET = 1024;
        protected const byte SIGN_SIZE = 0x01;
        protected const byte SIGN_PACKET_NUM = 0x02;
        protected const byte SIGN_PACKET_NUM_END = 0x03;
        protected Socket m_SockConn;
        //protected Semaphore m_Semaphore_Size;
        protected int ConnectToServer(string strServerIp)
        {
            try
            {
                int iPort = SERVER_PORT;
                IPAddress ip = IPAddress.Parse(strServerIp);
                IPEndPoint ipEnd = new IPEndPoint(ip, iPort);
                m_SockConn = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket
                m_SockConn.Connect(ipEnd);
            }
            catch (SocketException)
            {
                MessageBox.Show("socket exception in Client Conn!");
                return -1;
            }
            catch (ObjectDisposedException)
            {
                MessageBox.Show("Socket已关闭！ in Client Conn!");
                return -1;
            }
            //创建一个线程？ 接收数据。
            return 0;
        }
        protected void SendData(byte[] buffer, int iSize)
        {
            byte[] btSizePack = new byte[1024];
            //先发长度
            btSizePack[0] = SIGN_SIZE;
            byte[] btSize = BitConverter.GetBytes(iSize);
            for (int i = 0; i < btSize.Length; i++)
            {
                btSizePack[i + 1] = btSize[i];
            }
            m_SockConn.Send(btSize, btSize.Length + 1, 0);
            int iRemainSize = iSize;
            byte btPackNum = 0x00;
            byte[] btSendPack = new byte[1024];
            while (iRemainSize >= SIZE_OF_PACKET - 2)
            {
                btSendPack[0] = SIGN_PACKET_NUM;
                btSendPack[1] = btPackNum;
                int iSizeSend = iSize - iRemainSize;
                for (int i = 0; i < SIZE_OF_PACKET - 2; i++)
                {
                    btSendPack[i + 2] = buffer[iSizeSend + i];
                }
                m_SockConn.Send(btSendPack, SIZE_OF_PACKET, 0);
                iRemainSize -= SIZE_OF_PACKET;
                btPackNum = (byte)(btPackNum + 1);
            }
            //最后一块
            btSendPack[0] = SIGN_PACKET_NUM_END;
            btSendPack[1] = 0xff;
            for (int i = 0; i < iRemainSize; i++)
            {
                btSendPack[i + 2] = buffer[iSize - iRemainSize + i];
            }
            m_SockConn.Send(btSendPack, iRemainSize + 2, 0);
            //m_SockConn.Send(size, size.Length, 0);//发一个长度
            //m_Semaphore_Size.WaitOne();
            //          byte[] SignalAcpt=new byte[1024];
            //             int iRecv=m_SockConn.Receive(SignalAcpt, SignalAcpt.Length, 0);
            //             if(iRecv!=size.Length)//对方会返回发送数据
            //             {
            //                 MessageBox.Show("SignalAcpt Error in SendData!");
            //                 return;
            //             }
            //             for (int i = 0; i < iRecv;i++ )
            //             {
            //                 //比较。。
            //             }
        }
    }
}
